package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.*;
import cn.tedu.carehome.foundation.pojo.dto.OlderAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.OlderUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.Older;
import cn.tedu.carehome.foundation.pojo.vo.OlderFoodPackageStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.OlderListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.OlderStandardVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.service.IOlderService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class OlderServiceImpl implements IOlderService {
    @Autowired
    private OlderMapper olderMapper;

    @Autowired
    private IllMapper illMapper;

    @Autowired
    private CheckInfoMapper checkInfoMapper;

    @Autowired
    private VisitorMapper visitorMapper;

    @Autowired
    private FoodPackageMapper foodPackageMapper;

    @Override
    public void addNew(OlderAddNewDTO olderAddNewDTO) {
        log.debug("开始处理【添加老人健康信息】的业务，参数：{}", olderAddNewDTO);
        int count = olderMapper.countByName(olderAddNewDTO.getName());
        if (count > 0) {
            String message = "添加老人健康信息失败，老人名称已经被占有！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Older older = new Older();
        BeanUtils.copyProperties(olderAddNewDTO, older);
        log.debug("即将执行插入数据，参数：{}", older);
        olderMapper.insert(older);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除老人健康信息】的业务，参数：{}", id);

        {
            OlderStandardVO older = olderMapper.getStandardById(id);
            if (older == null) {
                String message = "删除老人信息失败，尝试访问的数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        {
            int count = illMapper.countByOlderId(id);
            if (count > 0) {
                String message = "删除老人信息失败，尝试访问老人存在关联的疾病数据！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        {
            int count = checkInfoMapper.countByOlderId(id);
            if (count > 0) {
                String message = "删除老人信息失败，尝试访问老人存在关联的入住登记信息数据！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        {
            Integer count = visitorMapper.countByOldId(id);
            if (count > 0) {
                String message = "删除老人信息失败，尝试访问老人存在关联的访问人员信息数据！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        {
            OlderFoodPackageStandardVO olderFoodPackageStandardVO = foodPackageMapper.selectLinkStandardByOlder(id);
            if (olderFoodPackageStandardVO != null) {
                String message = "删除老人信息失败，尝试访问老人存在关联套餐信息数据！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        log.debug("即将执行删除，参数：{}", id);
        olderMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        log.debug("开始【处理根据若干id批量删除老人信息】的业务，参数：{]", ids);

        for (int i = 0; i < ids.length; i++) {
            OlderStandardVO older = olderMapper.getStandardById(ids[i]);
            if (older == null) {
                String message = "删除老人信息失败，尝试访问的数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        int rows = olderMapper.deleteBatchByIds(ids);
        if (rows != ids.length) {
            String message = "删除老人信息失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

    @Override
    public void updateInfoById(OlderUpdateDTO olderUpdateDTO) {
        log.debug("开始处理【修改老人健康信息】的业务，新数据：{}",  olderUpdateDTO);
        {
            OlderStandardVO older = olderMapper.getStandardById(olderUpdateDTO.getId());
            if (older == null) {
                String message = "修改老人信息失败，尝试访问的数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        {
            int count = olderMapper.countByNameAndNotId(olderUpdateDTO.getName(), olderUpdateDTO.getId());
            if (count > 0) {
                String message = "修改老人信息失败，新的老人名称已经被占有！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        Older older = new Older();
        BeanUtils.copyProperties(olderUpdateDTO, older);
        log.debug("即将执行修改数据，参数：{}", older);
        olderMapper.update(older);
    }

    @Override
    public int countByContent(String content) {
        log.debug("开始处理【根据查询内容统计老人健康信息的数量】的业务，参数：{}", content);
        int count = olderMapper.countByContent(content);
        return count;
    }

    @Override
    public OlderStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询老人健康详情】业务，ID：{}", id);
        OlderStandardVO olderStandardVO = olderMapper.getStandardById(id);
        if (olderStandardVO == null) {
            String message = "查询老人健康详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回查询结果：{}", olderStandardVO);
        return olderStandardVO;
    }

    @Override
    public List<OlderListItemVO> list() {
        log.debug("开始处理【查询老人健康信息列表】业务，参数");
        List<OlderListItemVO> list = olderMapper.list();
        log.debug("即将返回查询结果：{}", list);
        return list;
    }

    @Override
    public List<OlderListItemVO> listLimit(int page, int size, String content) {
        log.debug("开始处理【分页查询老人健康信息列表】的业务，页数：{}，显示数量：{}，查询的内容：{}", page, size, content);
        List<OlderListItemVO> list = olderMapper.listLimit(page, size, content);
        return list;
    }

    @Override
    public JsonPage<OlderListItemVO> listOlderPage(Integer page, Integer pageSize) {
        log.debug("开始处理【分页查询老人信息】的业务，当前页码：{}，分页条数：{}", page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<OlderListItemVO> list = olderMapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }


}

